package com.orangelife.mobile.houserepair.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.common.util.OrangePhoneUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.houserepair.biz.RepairBiz;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 报修评价
 * Created by YangWei
 * on 2015/11/18.
 */
public class AddRepairJudgementActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.ivSettle)
    ImageView ivSettle;
    @Bind(R.id.ivUnSettle)
    ImageView ivUnSettle;
    @Bind(R.id.rbJudge)
    RatingBar rbJudge;
    @Bind(R.id.btnSubmit)
    Button btnSubmit;

    int status;
    String repID;
    Map<String, Object> map;
    int flag = 2;

    private Dialog submitDialog;

    public static boolean ISFRESH = false;

    RepairBiz repairBiz = RepairBiz.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_judgement);
        ButterKnife.bind(this);

        initView();
        findView();
    }

    /**
     * 初始化
     */
    private void initView() {
        map = new HashMap<>();
        Intent data = getIntent();
        map = (Map<String, Object>) data.getSerializableExtra("judge");
        status = Integer.parseInt(String.valueOf(map.get("status")));
        repID = String.valueOf(map.get("repID"));

        ivTitleRight.setVisibility(View.VISIBLE);
        ivTitleRight.setBackgroundResource(R.drawable.icon_phone);

        ISFRESH = false;

    }

    /**
     * 赋值并判断
     */
    private void findView() {
        if (status == 3) {
            tvTitle.setText("添加评价");
            ivSettle.setImageResource(R.drawable.noselsettle);
            ivUnSettle.setImageResource(R.drawable.noselunsettle);
            ivSettle.setOnClickListener(listener);
            ivUnSettle.setOnClickListener(listener);
            btnSubmit.setOnClickListener(listener);
            ivTitleRight.setOnClickListener(listener);
        } else if (status == 4) {
            tvTitle.setText("评价详情");
            btnSubmit.setVisibility(View.GONE);
            ivTitleRight.setOnClickListener(listener);
            rbJudge.setIsIndicator(true);
            if (!StringUtil.isContainBlank(String.valueOf(map.get("score")))) {
                rbJudge.setRating(Integer.parseInt(String.valueOf(map.get("score"))));
            }
            int isScore = 2;
            if (!StringUtil.isContainBlank(String.valueOf(map.get("isDone")))) {
                isScore = Integer.parseInt(String.valueOf(map.get("isDone")));
            }
            if (isScore == 1) {
                ivSettle.setImageResource(R.drawable.selsettle);
                ivUnSettle.setImageResource(R.drawable.noselunsettle);
            } else if (isScore == 0) {
                ivSettle.setImageResource(R.drawable.noselsettle);
                ivUnSettle.setImageResource(R.drawable.selunsettle);
            }

        }
    }

    /**
     * 设置监听器
     */
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_title_right:
                    OrangePhoneUtil.callCustomerService2(AddRepairJudgementActivity.this);
                    break;
                case R.id.ivSettle:
                    if (flag == 0 || flag == 2) {
                        ivSettle.setImageResource(R.drawable.selsettle);
                        ivUnSettle.setImageResource(R.drawable.noselunsettle);
                        flag = 1;
                    }
                    break;
                case R.id.ivUnSettle:
                    if (flag == 1 || flag == 2) {
                        ivUnSettle.setImageResource(R.drawable.selunsettle);
                        ivSettle.setImageResource(R.drawable.noselsettle);
                        flag = 0;
                    }
                    break;
                case R.id.btnSubmit:
                    int settle = 2;
                    if (flag == 1) {
                        settle = 1;
                    } else if (flag == 0) {
                        settle = 0;
                    }
                    if (settle != 2 && rbJudge.getRating() != 0) {
                        int score = (int)rbJudge.getRating();
                        Map<String, Object> mapAdd = new HashMap<>();
                        mapAdd.put("repID", repID);
                        mapAdd.put("score", score);
                        mapAdd.put("isDone", settle);
                        repairBiz.addRepairJudgement(mapAdd, new AddJudgeHandler(AddRepairJudgementActivity.this));
                        submitDialog = DialogHelper.getInstance().createLoadingDialog(
                                AddRepairJudgementActivity.this,
                                Constant.SUBMIT_LOADING);
                    } else {
                        ToastHelper.getInstance()._toast("请先评价后提交");
                    }
                    break;
            }
        }
    };

    static class AddJudgeHandler extends Handler {
        private WeakReference<AddRepairJudgementActivity> addJudgementActivity = null;

        public AddJudgeHandler(AddRepairJudgementActivity activity) {
            addJudgementActivity = new WeakReference<AddRepairJudgementActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            AddRepairJudgementActivity act = addJudgementActivity.get();

            if (act != null) {
                switch (msg.what) {
                    case 1:
                        String errInfo = JSONHelper.jsonToMap(msg.obj.toString())
                                .get("errInfo").toString();
                        if (errInfo.equals("OK")){
                            DialogHelper.closeDialog(act.submitDialog);
                            ToastHelper.getInstance()._toast(act.getResources().getString(R.string.submit_success));
                            ISFRESH = true;
                            act.finish();
                        }else{
                            DialogHelper.closeDialog(act.submitDialog);
                            ToastHelper.getInstance()._toast(
                                    act.getResources().getString(
                                            R.string.complaintes_sumbite_fail));
                        }
                        break;
                    case 500:
                        DialogHelper.closeDialog(act.submitDialog);
                        ToastHelper.getInstance()._toast(
                                msg.obj.toString());
                        break;
                    case 1011:
                        act.isLogin(act.submitDialog);
                        break;

                    default:
                        break;
                }
            }

        }
    }
}
